package com.springboot.template.thread.pool;


import java.util.concurrent.*;

public class Test {

    /**
     * newFixedThreadPool 固定大小线程池对象
     */
    void newFixedThreadPoolTest() {
        //创建一个固定大小的线程池，返回类型为ExecutorService
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 20; i++) {
            int finalI = i;
            //调用execute方法，参数必须传递Runnable对象
            //传递Runnable对象的三种方式
            //1.自己创建一个类实现Runnable接口 2.匿名内部类对象 3.lambda表达式，前提接口必须为函数式接口
            executorService.execute(()-> System.out.println(Thread.currentThread().getName() + "---->" + finalI));
        }
    }

    /**
     * 单一线程池
     */
    void newSingleThreadExecutorTest() {
        //创建单一线程池，返回类型ExecutorService
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        for (int i = 0; i < 50; i++) {
            executorService.execute(()-> System.out.println(Thread.currentThread().getName()+"======================"));
        }
    }

    /**
     * 可变线程池
      */
    void newCachedThreadPoolTest() {
        //创建可变大小的线程池，返回类型ExecutorService
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < 20; i++) {
            executorService.execute(()-> System.out.println(Thread.currentThread().getName()+"======================"));
        }
    }

    /**
     * 延迟线程池
     */
    void newScheduledThreadPoolTest() {
        System.out.println("start ======");
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);
        for (int i = 0; i < 5; i++) {
//            scheduledExecutorService.schedule(()->{
//                System.out.println(Thread.currentThread().getName()+"======================");
//            }, 5, TimeUnit.SECONDS);
            //有时间间隔
            scheduledExecutorService.scheduleAtFixedRate(()->{
                System.out.println(Thread.currentThread().getName()+"======================");
            },
            3,  //3秒后执行任务
            2,  //每隔2秒批量执行一次
            TimeUnit.SECONDS);
        }
        System.out.println("end ======");
    }


    void threadPoolExecutorTest() {
        //创建任务队列
        BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue(3);
        //创建线程池
        /**
         * int corePoolSize：核心线程数的个数
         *
         * int maximumPoolSize：最大线程数量
         *
         * long keepAliveTime：非核心线程允许空闲的时间
         *
         * TimeUnit unit：时间单位
         *
         * BlockingQueue workQueue：堵塞队列
         */
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 6, 6, TimeUnit.SECONDS, workQueue);
        for (int i = 0; i < 9; i++) {
            //调用submit方法，执行线程任务
            threadPoolExecutor.submit(()->{
                System.out.println(Thread.currentThread().getName()+"=====================");
            });
        }
    }


    public static void main(String[] args) {
        Test test = new Test();
        //ExecutorService 方式
//        test.newFixedThreadPoolTest();
//        test.newSingleThreadExecutorTest();
//        test.newCachedThreadPoolTest();
//        test.newScheduledThreadPoolTest();


        //ThreadPoolExecutor方式
        test.threadPoolExecutorTest();

    }
}
